package com.aspire.nm.component.miniServerDemo.clientTest;

import java.security.interfaces.RSAPublicKey;

import com.aspire.nm.component.miniServerDemo.Main;
import com.aspire.nm.component.util.*;


public class ServerTest_rsa {

    public static void main(String [] args) throws InterruptedException, InstantiationException, IllegalAccessException, BaseHttpClient.HttpClientException {
        new ServerTest_rsa().test();
    }
    
    private static String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCho/xlZX6qgdnWMcjjOb9r2K49PsacOUSRk3zg"+
            "Ify+usL8B+BbdougVAmyEyuhWbB62fcHmTX8kqtvGdXsput1YrT/gu8vTsMIx6+LoBgSFj5Z9BFP"+
            "/4ZnJl8f6M9C3hwYrB6VcYzWMZeRpHH+Zwtc2UlZ8fWJjToxBEKmcri/fQIDAQAB";
    
    private static RSAPublicKey publicKey;
    static{
        try {
            publicKey = RsaKeyUtil.string2publicKey(publicKeyStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void test() throws BaseHttpClient.HttpClientException {
        String postDate = "ha哈哈123";
        
        
        byte[] cipher = null;
        try {
            cipher = new RSAEncrypt().encrypt(publicKey, postDate.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String resp = new HttpTookit().doPost("http://"+Main.serverIp4Test+":7070/demo/rsa",BytesUtil.bytes2hex(cipher),null,null,"UTF-8");

        byte[] plainText = null;
        try {
            plainText = new RSAEncrypt().decrypt(publicKey, BytesUtil.hex2bytes(resp));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(new String(plainText));
        
    }
    
}

